package com.example.helloandroidworld;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MessagingSMS extends Activity
{
	private Button goSendSMS, myContacts;
	private EditText numPhone, textMessage;
	
	@Override
	public void onCreate( Bundle savedInstanceState )
	{ 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.messagingsms);
		setUpButtons(); 

	}
	
	private void setUpButtons()
	{
		// use the view variable to bind it to the numPhone, textMessage, and goSensSMS java code
		numPhone = (EditText) findViewById(R.id.txtPhoneNo);
		textMessage =(EditText) findViewById(R.id.txtMessage);
		goSendSMS = (Button) findViewById(R.id.btnSendSMS);
		myContacts = (Button) findViewById(R.id.btnContacts);
		
		// set-up the onclick listener
		goSendSMS.setOnClickListener(new View.OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				String phoneNum = numPhone.getText().toString();
				String messageText = textMessage.getText().toString();
				
				if( phoneNum.length() > 0 && messageText.length() > 0 )
						sendSMS( phoneNum, messageText);
				else
					Toast.makeText(getBaseContext(), "Enter all request input", Toast.LENGTH_SHORT).show();}});
		
		myContacts.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) { // transitions screens
				startActivity( new Intent(MessagingSMS.this, MessagingContacts.class)); }});
	}
	private void sendSMS( String phoneNumber, String message)
	{
		PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, Messaging.class), 0);
		SmsManager managerTool = SmsManager.getDefault();
		managerTool.sendTextMessage(phoneNumber, null, message, pi, null);
		//finish();
	}
}